package com.igeek.service;

import com.igeek.dao.CategoryDao;
import com.igeek.dao.ProductDao;
import com.igeek.domain.Category;
import com.igeek.domain.Pagination;
import com.igeek.domain.Product;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @author jerryHe
 * @create 2019-10-09 16:18
 */
public class ProductService {
    ProductDao dao = new ProductDao();

    public List<Product> findHistoryProducts(String pids) {
        //1,29,2,19,17,13,11
        List<Product> products = new LinkedList<>();
        String[] srr = pids.split(",");
        for (String pid : srr){
            Product p = dao.getProductByPid(pid);
            products.add(p);
        }

        return  products;

    }

    public List<Product> findProductsByCid(String cid) {
        return dao.findProductsByCid(cid);
    }

    public Pagination<Product> findProductsByCidPage(int nowPage, int i,String cid) {
        Pagination<Product> pagination = new Pagination<>();
        //1.获取数据
        List<Product> data = dao.findData(nowPage,i,cid);
        pagination.setData(data);
        //2.获取总记录数
        int record = dao.getCount(cid);
        //3.计算总页数
        int pages =(int)Math.ceil((double)record/i);
        pagination.setPage(nowPage);
        pagination.setCount(record);
        pagination.setTotalPage(pages);
        //4.设置分页条
        pagination.setBar();


        return pagination;
    }

    public List<Product> getHotProducts() {
        return dao.getHotProducts();
    }

    public List<Product> getNewProducts() {
        return dao.getNewProducts();
    }

    public Product getProductByPid(String pid) {
        return dao.getProductByPid(pid);
    }
}
